import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';
import 'package:flutter/services.dart';
import 'ForgetPWDViewModel.dart';
import 'bean/StringData.dart';

class ForgetPWD2 extends BaseStatefulWidget {
  Map map;

  ForgetPWD2(this.map);

  @override
  State<StatefulWidget> initState() {
    return ForgetPWD2Sate();
  }
}

class ForgetPWD2Sate extends BaseState<ForgetPWDViewModel, ForgetPWD2> {
  Widget _tfPassword;
  String _passWord = "";
  Widget _tfPasswordAgain;
  String _passWordAgain = "";
  Widget _rbReset;
  bool _showMiMa = false;
  bool _showMiMaAgain = false;
  String _errorText;
  String _errorTextAgain;

  @override
  Widget initWidget() {
    initView();
    return Scaffold(
      appBar: MTopBar(title: "找回密码").create(),
      body: _bodView(),
      // resizeToAvoidBottomPadding: false, //输入框抵住软键盘
    );
  }

  void initView() {
    _tfPassword = TextField(
      inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp("[0-9]|[a-z]|[A-Z]")),
      ],
      style: TextStyle(color: SQColor.per87Black, fontSize: Dimens.textSize32),
      controller: TextEditingController.fromValue(
        TextEditingValue(
            text: _passWord,
            selection: TextSelection.fromPosition(TextPosition(
                affinity: TextAffinity.downstream, offset: _passWord.length))),
      ),
      decoration: InputDecoration(
        labelText: "新密码",
        suffixIcon: IconButton(
          icon: Icon(
            _showMiMa ? Icons.visibility : Icons.visibility_off,
            color: SQColor.black_282828,
          ),
          onPressed: () => _isShowMiMa(),
        ),
        errorText: _errorText,
      ),
      obscureText: !_showMiMa,
      maxLength: 16,
      onChanged: (str) {
        setState(() {
          _passWord = str;
          if (_passWord != null &&
              _passWord.length >= 6 &&
              _passWord.length <= 16) {
            _errorText = null;
            if (_passWord == _passWordAgain) {
              _errorTextAgain = null;
            } else {
              _errorTextAgain = "两次输入不一致";
            }
          } else {
            _errorText = "请输入6-16位密码";
          }
        });
      },
    );
    _tfPasswordAgain = TextField(
      inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp("[0-9]|[a-z]|[A-Z]")),
      ],
      style: TextStyle(color: SQColor.per87Black, fontSize: Dimens.textSize32),
      controller: TextEditingController.fromValue(
        TextEditingValue(
            text: _passWordAgain,
            selection: TextSelection.fromPosition(TextPosition(
                affinity: TextAffinity.downstream,
                offset: _passWordAgain.length))),
      ),
      decoration: InputDecoration(
        labelText: "确认新密码",
        suffixIcon: IconButton(
          icon: Icon(
            _showMiMaAgain ? Icons.visibility : Icons.visibility_off,
            color: SQColor.black_282828,
          ),
          onPressed: () => _isShowMiMaAgain(),
        ),
        errorText: _errorTextAgain,
      ),
      obscureText: !_showMiMaAgain,
      maxLength: 16,
      onChanged: (str) {
        setState(() {
          _passWordAgain = str;
          if (_passWordAgain != null &&
              _passWordAgain.length >= 6 &&
              _passWordAgain.length <= 16) {
            if (_passWord == _passWordAgain) {
              _errorTextAgain = null;
            } else {
              _errorTextAgain = "两次输入不一致";
            }
          } else {
            _errorTextAgain = "请输入6-16位密码";
          }
        });
      },
    );
    _rbReset = MaterialButton(
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(Dimens.dimens8))),
      color: SQColor.theme_color,
      textColor: SQColor.white,
      disabledColor: SQColor.black_b2b2b2,
      disabledTextColor: SQColor.black_8e8e8e,
      minWidth: double.infinity,
      height: Dimens.dimens48,
      onPressed: () => _reset(),
      child: Text(
        "确认",
        style: TextStyle(
          fontSize: Dimens.textSize28,
        ),
      ),
    );
  }

  _bodView() {
    return Padding(
      padding: EdgeInsets.fromLTRB(
          Dimens.dimens32, Dimens.dimens64, Dimens.dimens32, 0),
      child: Column(
        children: <Widget>[
          _tfPassword,
          Container(height: Dimens.dimens16),
          _tfPasswordAgain,
          Container(height: Dimens.dimens64),
          _rbReset,
        ],
      ),
    );
  }

  // 找回密码
  _reset() {
    if (_passWord == null || _passWord.length < 6 || _passWord.length > 16) {
      _errorText = "请输入6-16位密码";
      return;
    }
    if (_passWordAgain == null ||
        _passWordAgain.length < 6 ||
        _passWordAgain.length > 16) {
      _errorTextAgain = "请输入6-16位密码";
      return;
    }
    if (_passWord != _passWordAgain) {
      ToastUtils.showToast("两次密码输入不一致");
      return;
    }
    viewModel.resetPwd(
        widget.map["phone"], widget.map["token"], _passWordAgain);
    viewModel.resetPwdrStream().listen((result) {
      StringData strData = result;
      if (strData != null && strData.code == 0) {
        // 修改密码成功
        _success(context);
      } else if (strData == null) {
        // 密码修改失败
        ToastUtils.showToast("修改失败");
      }
    });
  }

  _isShowMiMa() {
    setState(() {
      _showMiMa = !_showMiMa;
    });
  }

  _isShowMiMaAgain() {
    setState(() {
      _showMiMaAgain = !_showMiMaAgain;
    });
  }
BuildContext dialogContext;
  // 提示框
  _success(BuildContext context) {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (context) {
          dialogContext=context;
          return WillPopScope(
            child: _dialogBody(),
            onWillPop: () => _dialogBack(),
          );
        });
  }

  _dialogBack() {
    Navigator.pop(dialogContext); //关闭dialog
    Navigator.pop(context, "success"); //关闭页面
  }

  _dialogBody() {
    return Center(
      child: Stack(
        alignment: Alignment.topCenter,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.only(top: Dimens.dimens32),
            child: Container(
              margin: EdgeInsets.only(
                  left: Dimens.dimens32, right: Dimens.dimens32),
              child: Card(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: Container(),
                        ),
                        IconButton(
                          icon: Icon(Icons.close, color: SQColor.black_999999),
                          onPressed: () {
                            _dialogBack();
                          },
                        ),
                      ],
                    ),
                    Container(height: Dimens.dimens24),
                    Text("修改成功！",
                        style: TextStyle(
                            fontSize: Dimens.textSize36,
                            color: SQColor.black_333333)),
                    Container(height: Dimens.dimens16),
                    Text("请使用新密码进行登录",
                        style: TextStyle(
                            fontSize: Dimens.textSize24,
                            color: SQColor.black_666666)),
                    Padding(
                      padding: EdgeInsets.all(Dimens.dimens32),
                      child: MaterialButton(
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.all(
                                Radius.circular(Dimens.dimens8))),
                        color: SQColor.theme_color,
                        textColor: SQColor.white,
                        disabledColor: SQColor.black_b2b2b2,
                        disabledTextColor: SQColor.black_8e8e8e,
                        height: Dimens.dimens48,
                        minWidth: double.infinity,
                        onPressed: () => _dialogBack(),
                        child: Text(
                          "返回登录",
                          style: TextStyle(fontSize: Dimens.textSize28),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
          Image.asset("img/img_success.png",
              width: Dimens.dimens100, height: Dimens.dimens100),
        ],
      ),
    );
  }
}
